/*
 * 牛牛主场景
 * author: sfmy
 */
var NNModel           = require('./NNModel');
let roomAPI           = require('../../API/RoomAPI');
var NNProto           = require('./NNProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var enumeration       = require('../../Constant/enumeration');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
		robBankButtons:    cc.Node,
		pourScoresButtons: cc.Node,
		readyButton:       cc.Node,
		showCardsButton:   cc.Node,
		roomIdLabel:       cc.Label,
		bureauLabel:       cc.Label,
		cuopaiPrefab:      cc.Prefab,
		chatAnim:          cc.Prefab,
	},

	onLoad: function () {
		this.headNodeArray = [];
		this.cardsNodeArray = [];
		NNModel.init(); /* 保存数据 */ 
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
        Global.MessageCallback.addListener('GAME_EVENT', this);
        Global.MessageCallback.addListener('UpdateGameBg', this);

		/* 获取场景 */
        this.scheduleOnce(function () {
            RoomAPI.roomMessageNotify(RoomProto.getRoomSceneInfoNotify());
        }, 0.2);
        /* Global.AudioManager.startPlayBgMusic('Game/Niuniu/NNAudio/back'); */
		this.updateBgSprite();
		if (cc.sys.localStorage.getItem('gameMusic') == "gameMusic2") {
			Global.AudioManager.startPlayBgMusic("Game/Common/Audio/game_bg_2");
		}
		else {
			Global.AudioManager.startPlayBgMusic("Game/Common/Audio/game_bg_1");
		}
	},

	/*
	 * 游戏初始化
	 * 断线重连
	 */
	gameInit () {
		this.setHeadAndCard();
		this.roomIdLabel.string = '房号：'+NNModel.getRoomID();
		this.bureauLabel.string = '局数：' +NNModel.getCurBureau()+'/'+NNModel.getMaxBureau()+'局';

		let user = NNModel.getUserByChairID(NNModel.getMyChairID());
		if (user) {
			if (NNModel.getGameStatus() == NNProto.gameStatus.NONE && !(user.userStatus&RoomProto.userStatusEnum.READY) && NNModel.getMyChairID() < NNModel.getChairCount()) {
				this.readyButton.active = true;
			}
			else {
				this.readyButton.active = false;
			}
			this.showCardsButton.active = false;
			cc.find('cuopai', this.node).active = false;
			if (NNModel.getGameStatus() == NNProto.gameStatus.SHOW_CARDS) {
				let cards = NNModel.getHandCardsByChairID(NNModel.getMyChairID());
				if (!NNModel.getShowCards(NNModel.getMyChairID()) && NNModel.getIsPlayingByChairID(NNModel.getMyChairID())) {
					this.showCardsButton.active = true;
					/* TODO 判断搓牌 */
				}
			}
		}
		cc.find('Tuoguan', this.node).active = NNModel.getTrustByChairID(NNModel.getMyChairID());
		cc.find('Pangguan', this.node).active = (NNModel.getMyChairID() >= NNModel.getChairCount());
		/* cc.find('Setting/Layout/TuoButton', this.node).active = NNModel.getGameRule().canTrust; */
		cc.find('Setting/Layout/TuoButton', this.node).active = true;
		/* cc.find('copy', this.node).active = (NNModel.getCurBureau() == 0);
		cc.find('invite', this.node).active = (NNModel.getCurBureau() == 0); */
		cc.find('backhall', this.node).active = (NNModel.getCurBureau() == 0);
		cc.find('Voice', this.node).active = NNModel.getGameRule().yuyin;
	},

	addHeadClickEvent: function (node) {
		node.on(cc.Node.EventType.TOUCH_START, () => {
			if (NNModel.getMyChairID() >= NNModel.getChairCount()) {
				Global.DialogManager.addTipDialog('观战玩家无法查看详细信息');
				return;
			}
			if (node.lastClick && Date.now()-node.lastClick < 500) { /* 防止重复点击 */ 
				return;
			}
			node.lastClick = Date.now();
			let logic = node.parent.getComponent('NNHead');
			let user = NNModel.getUserByChairID(logic.getChairID());
			let address = null;
			try { address = JSON.parse(user.userInfo.address); } catch(e) {}
			let params = {
				fromChairID: NNModel.getMyChairID(),
				toChairID: logic.getChairID(),
				avatar: user.userInfo.avatar,
				id: user.userInfo.uid,
				ip: user.userInfo.lastLoginIP,
				location: user.userInfo.location,
				x: address? address.x:'未知',
				y: address? address.y:'未知',
				gold: user.userInfo.score,
				name: user.userInfo.nickname,
			};
			Global.DialogManager.createDialog('Chat/HeadDetail', params);
		}, this);
	},

	messageCallbackHandler: function(router, msg) {
		if(!NNModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				console.log('gameInit=============================');
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_RESPONSE) {
				if(msg.data.chairID === NNModel.getMyChairID()) {
					Global.DialogManager.removeLoadingCircle();
				}
			}
			else if(msg.type === RoomProto.OTHER_USER_ENTRY_ROOM_PUSH) {
				this.showHeadNode(msg.data.roomUserInfo.chairID);
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_PUSH) {
				this.hideHeadNode(msg.data.roomUserInfo.chairID);
			}
			else if(msg.type === RoomProto.USER_READY_PUSH) {
				this.userReadyPush(msg.data.chairID);
			}
			else if(msg.type === RoomProto.GAME_END_PUSH) {
			}
			else if(msg.type === RoomProto.ROOM_DISMISS_PUSH) {
			}
			else if(msg.type === RoomProto.ASK_FOR_DISMISS_PUSH) {
				if (!Global.DialogManager.isDialogExit('Game/DismissNode')) {
					msg.data.myChairID = NNModel.getMyChairID();
					Global.DialogManager.createDialog('Game/DismissNode', msg.data, function (dialog) {
						Global.DialogManager.removeLoadingCircle();
					});
				}
			}
			else if(msg.type === RoomProto.USER_CHANGE_SEAT_PUSH) {
				let user = NNModel.getUserByChairID(NNModel.getMyChairID());
				if (NNModel.getGameStatus() == NNProto.gameStatus.NONE && !(user.userStatus&RoomProto.userStatusEnum.READY) && NNModel.getMyChairID() < NNModel.getChairCount()) {
					this.readyButton.active = true;
				}
				else {
					this.readyButton.active = false;
				}
				cc.find('Pangguan', this.node).active = (NNModel.getMyChairID() >= NNModel.getChairCount());
			}
			else if(msg.type === RoomProto.USER_CHAT_PUSH) {
				this.onChat(msg.data.fromChairID, msg.data.toChairID, msg.data.msg);
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === NNProto.GAME_BUREAU_PUSH) {
				this.bureauLabel.string = '局数：'+NNModel.getCurBureau()+'/'+NNModel.getMaxBureau();
			}
			else if(msg.type === NNProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus == NNProto.gameStatus.SHOW_CARDS) {
					this.showCardsButton.active = NNModel.getIsPlayingByChairID(NNModel.getMyChairID());
					cc.find('cuopai', this.node).active = NNModel.getIsPlayingByChairID(NNModel.getMyChairID()) && NNModel.getGameRule().cuopai;
				}
				else {
					this.showCardsButton.active = false;
					cc.find('cuopai', this.node).active = false;
				}
				let user = NNModel.getUserByChairID(NNModel.getMyChairID());
				if (NNModel.getGameStatus() == NNProto.gameStatus.NONE && !(user.userStatus&RoomProto.userStatusEnum.READY) && NNModel.getMyChairID() < NNModel.getChairCount()) {
					if (NNModel.getGameStatus() == NNProto.gameStatus.NONE && !(user.userStatus&RoomProto.userStatusEnum.READY) && NNModel.getMyChairID() < NNModel.getChairCount() && NNModel.getCurBureau() < NNModel.getMaxBureau()) {
						this.readyButton.active = true;
					}
				}
				/* cc.find('copy', this.node).active = false;
				cc.find('invite', this.node).active = false; */
				cc.find('backhall', this.node).active = false;
			}
			else if(msg.type === NNProto.GAME_SHOW_CARDS_PUSH) {
				if (msg.data.chairID == NNModel.getMyChairID()) {
					if (msg.data.cuopai) { /* 搓牌结束 */ 
						this.showCardsButton.active = NNModel.getIsPlayingByChairID(NNModel.getMyChairID());
						cc.find('cuopai', this.node).active = NNModel.getIsPlayingByChairID(NNModel.getMyChairID()) && NNModel.getGameRule().cuopai;
					}
					else {
						this.showCardsButton.active = false;
						cc.find('cuopai', this.node).active = false;
					}
				}
			}
			else if(msg.type === NNProto.GAME_END_PUSH) {
				if (msg.data.result.length > 0) {
					msg.data.enterHall = this.enterHall.bind(this);
					Global.DialogManager.createDialog('Game/Niuniu/NNResult', msg.data, function (dialog) {
						Global.DialogManager.removeLoadingCircle();
					});
				}
				else {
					this.enterHall();
				}
			}
			else if(msg.type === NNProto.GAME_TRUST_PUSH) {
				if (msg.data.chairID == NNModel.getMyChairID()) {
					cc.find('Tuoguan', this.node).active = msg.data.trust;
				}
			}
			else if(msg.type === NNProto.GAME_REVIEW_PUSH) {
				if (msg.data.list.length != 0) {
					Global.DialogManager.createDialog('Game/Niuniu/NNReview', msg.data);
				}
				else {
					Global.DialogManager.addTipDialog('没有数据');
				}
			}
			else if(msg.type === NNProto.GAME_RESULT_PUSH) {
				for (let node of cc.find('CuopaiNode', this.node).children) {
					node.destroy();
				}
			}
		}
		else if(router === 'ReConnectSuccess' || (router == 'GAME_EVENT' && msg == cc.game.EVENT_SHOW)) {
			console.log('断线重连=====================');
			if(Global.UserModel.isInRoom()) {
				Global.API.hall.joinRoomRequest(NNModel.getRoomID(), function() {
					NNModel.setGameInited(false);
					roomAPI.roomMessageNotify(RoomProto.getRoomSceneInfoNotify());
				}, function () {
					// Global.DialogManager.destroyDialog('Game/Niuniu/NNMainDialog');
					this.enterHall();
				}.bind(this));
			} else {
				// Global.DialogManager.destroyDialog('Game/Niuniu/NNMainDialog');
				this.enterHall();
			}
		}
		else if(router == 'UpdateGameBg') {
			this.updateBgSprite();
		}
	},

	updateBgSprite: function () {
		let bgUrl = cc.sys.localStorage.getItem('gameBg');
		let title = (!bgUrl || bgUrl == 'gameBg1')? 'Game/Niuniu/niuniudiwen':'Game/Niuniu/niuniudiwen1';
		bgUrl = (!bgUrl || bgUrl == 'gameBg1')? 'Game/Common/beijinglan':'Game/Common/beijinglv';
		Global.CCHelper.updateSpriteFrame(title, cc.find('background/diwen', this.node).getComponent(cc.Sprite));
		Global.CCHelper.updateSpriteFrame(bgUrl, cc.find('background', this.node).getComponent(cc.Sprite));
	},

	/*
	 * 显示玩家头像
	 */
	showHeadNode: function (chairID) {
		/* this.getHeadNodeByChairID(chairID).active = true; */
	},

	/*
	 * 隐藏玩家头像
	 */
	hideHeadNode: function (chairID) {
		if(chairID === NNModel.getMyChairID()) {
			this.enterHall();
		}
	},

	/*
	 * 根据chairid获取头像节点
	 * @param chairID 椅子id
	 */
	getHeadNodeByChairID: function (chairID) {
		for (let node of this.headNodeArray) {
			if (node.getComponent('NNHead').getChairID() == chairID) {
				return node;
			}
		}
		return null;
	},

	/*
	 * 根据chairid获取手牌节点
	 * @param chairID 椅子id
	 */
	getCardsNodeByChairID: function (chairID) {
		let chairCount = NNModel.getChairCount();
		let index = (chairID+chairCount-NNModel.getMyChairID())%chairCount;
		return this.cardsNodeArray[index];
	},

	/*
	 * 玩家准备推送
	 */
	userReadyPush: function (chairID) {
		if (chairID == NNModel.getMyChairID()) {
			this.readyButton.active = false;
		}
	},

	onReadyClick: function (event, param) {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		Global.NetworkManager.notify(RoomMessageRouter, RoomProto.userReadyNotify(true));
		this.readyButton.active = false;
	},

	onShowCardsClick: function (event, param) {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		if (param == 0) {
			Global.NetworkManager.notify(GameMessageRouter, NNProto.gameShowCardsNotify());
		}
		else {
			let params = {
				card: NNModel.getHandCardsByChairID(NNModel.getMyChairID())[4],
				cb: () => { Global.NetworkManager.notify(GameMessageRouter, NNProto.gameShowCardsNotify()); },
			};
			/* Global.DialogManager.createDialog('Game/CuopaiNode', param, function (dialog) {
				Global.DialogManager.removeLoadingCircle();
			}); */
			let node = cc.instantiate(this.cuopaiPrefab);
			node.parent = cc.find('CuopaiNode', this.node);
			node.y = 20;
			node.getComponent('CuopaiNode').init(params.card, params.cb);
			cc.find('cuopai', this.node).active = false;
		}
		this.showCardsButton.active = false;
	},

	onExitClick: function (event, param) {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		if (param != 'backhall') {
			this.onSwitchClick();
		}
		let user = NNModel.getUserByChairID(NNModel.getMyChairID());
		let canDismiss = ((user.userStatus&RoomProto.userStatusEnum.DISMISS) != 0 && user.chairID < NNModel.getChairCount());
		if (canDismiss && NNModel.getGameStarted()) {
			Global.DialogManager.addPopDialog('是否确定发出解散申请？', function() {
				Global.NetworkManager.notify(RoomMessageRouter, RoomProto.getAskForDismissNotifyData(true));
				Global.DialogManager.addLoadingCircle();
			}, function() {});
		}
		else {
			Global.DialogManager.addPopDialog('确认退出游戏?', function() {
				Global.NetworkManager.notify(RoomMessageRouter, RoomProto.userLeaveRoomNotify());
				Global.DialogManager.addLoadingCircle();
			}, function() {});
		}
	},

	onDestroy: function () {
		NNModel.onDestroy();
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
        Global.MessageCallback.removeListener('GAME_EVENT', this);
        Global.MessageCallback.removeListener('UpdateGameBg', this);
	},

	enterHall: function () {
		if(!NNModel.getGameInited()) { return; }
		let isUnionCreate = NNModel.isUnionCreate();
		Global.DialogManager.destroyDialog('Game/Niuniu/NNMainDialog');
		if (isUnionCreate) {
			Global.DialogManager.createDialog("UI/Union/UnionMain/UnionMainDialog", {unionID: NNModel.getRoomCreator().unionID});
		}
		else {
			Global.DialogManager.createDialog('UI/Hall/HallDialog', null, function () {
				Global.DialogManager.removeLoadingCircle();
			});
		}
	},

	onSwitchClick: function () {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		let user = NNModel.getUserByChairID(NNModel.getMyChairID());
		let canDismiss = ((user.userStatus&RoomProto.userStatusEnum.DISMISS) != 0 && user.chairID < NNModel.getChairCount());
		let sprite = cc.find('Setting/bg/DismissButton/Sprite', this.node).getComponent(cc.Sprite);
		if (canDismiss && NNModel.getGameStarted()) {
			Global.CCHelper.updateSpriteFrame('Game/Glob/dismiss', sprite);
		}
		else {
			Global.CCHelper.updateSpriteFrame('Game/Glob/leaveroom', sprite);
		}

		let bg = cc.find('Setting/bg', this.node);
		let url = 'Game/Glob/button1';
		let node = cc.find('Setting/Button/Background', this.node);
		if (!bg.active) {
			url = 'Game/Glob/button2';
			bg.active = true;
			Global.CCHelper.updateSpriteFrame(url, node.getComponent(cc.Sprite));
		}
		else {
			Global.CCHelper.updateSpriteFrame(url, node.getComponent(cc.Sprite));
			bg.active = false;
		}
	},

	onTrustClick: function () {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		Global.NetworkManager.notify(GameMessageRouter, NNProto.gameTrushNotify(true));
	},

	onSettingClick: function () {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		Global.DialogManager.createDialog("UI/Setting/SettingDialog");
		this.onSwitchClick();
	},

	onRuleClick: function () {
		if(!NNModel.getGameInited()) { return; }
        Global.AudioManager.playCommonSoundClickButton();
		let node = cc.find('Sprite/bg1/RuleButton', this.node);
		if (node.lastClick && Date.now()-node.lastClick < 500) { /* 防止重复点击 */ 
			return;
		}
		node.lastClick = Date.now();
		Global.DialogManager.createDialog('Game/RuleNode', this.getRule());
	},

	getRule: function () {
		let gameRule = NNModel.getGameRule();
		let rule = {
			'模式：': ['', '牛牛上庄', '自由抢庄', '轮庄', '通比', '房主坐庄', '明牌抢庄'][gameRule.gameFrameType],
			'玩法：': ['', '全暗', '明3暗2', '明4暗1'][gameRule.sendCardType],
			'翻倍：': ['', '牛牛(x4)牛九(x3)牛八(x2)牛七(x2)', '牛牛(x3)牛九(x2)牛八(x2)', '点子牛(倍数与点数对应)'][gameRule.scaleType],
			'高级：': (gameRule.cuopai? '允许搓牌':'') + (gameRule.canTrust? ' 允许托管':'') + (gameRule.canEnter? ' 中途进人':'') + (gameRule.canWatch? ' 允许观战':'') + (gameRule.canTrust? '托管':'不托管'),
			'特殊：': (gameRule.cardsType.TONGHUASHUN? '同花顺(10倍)':'') + (gameRule.cardsType.YITIAOLONG? ' 一条龙(9倍)':'') + (gameRule.cardsType.ZHADANNIU? ' 炸弹牛(8倍)':'') + (gameRule.cardsType.WUXIAONIU? ' 五小牛(7倍)':'') + (gameRule.cardsType.HULUNIU? ' 葫芦牛(6倍)':'') + (gameRule.cardsType.WUHUANIU? ' 五花牛(6倍)':'') + (gameRule.cardsType.TONGHUANIU? ' 同花牛(5倍)':'') + (gameRule.cardsType.SHUNZINIU? ' 顺子牛(4倍)':''),
		};
		if (gameRule.scaleType == NNProto.scaleType.DIANZI) {
			rule['特殊：'] = (gameRule.cardsType.TONGHUASHUN? '同花顺(16倍)':'') + (gameRule.cardsType.YITIAOLONG? ' 一条龙(15倍)':'') + (gameRule.cardsType.ZHADANNIU? ' 炸弹牛(14倍)':'') + (gameRule.cardsType.WUXIAONIU? ' 五小牛(13倍)':'') + (gameRule.cardsType.HULUNIU? ' 葫芦牛(12倍)':'') + (gameRule.cardsType.WUHUANIU? ' 五花牛(11倍)':'') + (gameRule.cardsType.TONGHUANIU? ' 同花牛(11倍)':'') + (gameRule.cardsType.SHUNZINIU? ' 顺子牛(11倍)':'');
		}
		if (rule['特殊：'].length == 0) {
			rule['特殊：'] = '无';
		}
		if (NNModel.isUnionCreate() && NNModel.getRobBankLimit() > 0) {
			rule['抢庄：'] = `抢庄玩家金币不能少于${NNModel.getRobBankLimit()}`;
		}
		return rule;
	},

	onCancelTuoguan: function () {
		if(!NNModel.getGameInited()) { return; }
		Global.AudioManager.playCommonSoundClickButton();
		Global.NetworkManager.notify(GameMessageRouter, NNProto.gameTrushNotify(false));
	},

	onCopyRoomID: function () {
		if(!NNModel.getGameInited()) { return; }
		Global.AudioManager.playCommonSoundClickButton();
		let str = '牛牛---房间号：' + NNModel.getRoomID() + ' ';
		let rule = this.getRule();
		for (let key in rule) {
			str += key + rule[key] + ' ';
		}
		console.log(str);
		Global.PlatformHelper.copyText(str);
	},

	onInviteFriend: function () {
		if(!NNModel.getGameInited()) { return; }
		Global.AudioManager.playCommonSoundClickButton();
		console.log('onInviteFriend');
	},

	onReview: function () {
		if(!NNModel.getGameInited()) { return; }
		Global.AudioManager.playCommonSoundClickButton();
		Global.NetworkManager.notify(GameMessageRouter, NNProto.gameReviewNotify());
	},

	onChat: function(fromChairID, toChairID, msg) {
		if(!NNModel.getGameInited()) { return; }
		if (fromChairID == toChairID) {
			let url = 'Chat/vipEmoj/bailaoye/';
			let audio = 'Chat/ChatAnimSound/bailaoye_nan';
			let begin = 1, end = 11;
			if (msg == 22) {
				url = 'Chat/vipEmoj/caishen/';
				audio = 'Chat/ChatAnimSound/caishen_nan';
				begin = 1; end = 22;
			}
			else if (msg == 23) {
				url = 'Chat/vipEmoj/dajidali/';
				audio = 'Chat/ChatAnimSound/dajidali_nan';
				begin = 1; end = 23;
			}
			else if (msg == 24) {
				url = 'Chat/vipEmoj/haoqiyo/';
				audio = 'Chat/ChatAnimSound/haoqiyo_nan';
				begin = 1; end = 12;
			}
			else if (msg == 25) {
				url = 'Chat/vipEmoj/shengqile/';
				audio = 'Chat/ChatAnimSound/shengqile_nan';
				begin = 1; end = 8;
			}
			Global.AudioManager.playSound(audio);

			let node = new cc.Node();
			let sprite = node.addComponent(cc.Sprite);
			let animation = node.addComponent(cc.Animation);
			let urls = [];
			for (let i = begin; i <= end; ++i) {
				urls.push(url+i);
			}
			cc.loader.loadResArray(urls, cc.SpriteFrame, (err, frames) => {
				if (err) {
					console.log(urls, 'load error');
					return;
				}
				let clip = cc.AnimationClip.createWithSpriteFrames(frames, frames.length);
				clip.name = 'emotion';
				clip.speed = 0.5;
				animation.addClip(clip);
				animation.play('emotion');
				node.parent = this.node;
				node.runAction(cc.sequence(
					cc.delayTime(clip.duration+1),
					cc.removeSelf()
				));
			});
		}
		else {
			let node = cc.instantiate(this.chatAnim);
			node.parent = this.node;
			let logic = node.getComponent('ChatAnimal');
			logic.playAnimal(msg);
			let startPos = this.getHeadNodeByChairID(fromChairID).getPosition();
			let endPos = this.getHeadNodeByChairID(toChairID).getPosition();
			node.setPosition(startPos);
			node.runAction(cc.moveTo(0.5, endPos));
		}
	},

	onVoice: function () {
		if(!NNModel.getGameInited()) { return; }
		Global.AudioManager.playCommonSoundClickButton();
		console.log('onVoice');
	},

	onRefrush: function (event) {
		if(!NNModel.getGameInited()) { return; }
		if (event.target.lastClick && Date.now()-event.target.lastClick < 10*1000) {
			Global.DialogManager.addTipDialog('请勿频繁操作');
			return;
		}
		Global.DialogManager.addTipDialog('刷新界面可以修复游戏显示问题');
		event.target.lastClick = Date.now();
		Global.AudioManager.playCommonSoundClickButton();
		console.clash("niuniu-onRefrush", NNModel.getRoomData());
		this.messageCallbackHandler('ReConnectSuccess');
	},

	setHeadAndCard: function () {
		let chairCount = NNModel.getGameRule().maxPlayerCount;
		let headPosArray = [cc.v2(-586, -293), cc.v2(606, -40), cc.v2(561, 141), cc.v2(199, 261), cc.v2(-37, 303), cc.v2(-261, 262), cc.v2(-568, 141), cc.v2(-605, -37)];
		let cardPosArray = [cc.v2(35, -245), cc.v2(416, -88), cc.v2(373, 92), cc.v2(166, 112), cc.v2(-42, 154), cc.v2(-275, 116), cc.v2(-470, 92), cc.v2(-508, -85)];
		let choosedArray = [1, 2, 3, 4, 5, 6, 7, 8];
		if (chairCount == 6) {
			headPosArray = [cc.v2(-586, -293), cc.v2(606, -14), cc.v2(310, 248), cc.v2(-37, 303), cc.v2(-359, 249), cc.v2(-605, -11)];
			cardPosArray = [cc.v2(35, -245), cc.v2(416, -62), cc.v2(277, 99), cc.v2(-42, 154), cc.v2(-355, 103), cc.v2(-508, -59)];
			choosedArray = [1, 2, 4, 5, 6, 8];
		}
		else if (chairCount == 10) {
			headPosArray = [cc.v2(-586, -293), cc.v2(606, -99), cc.v2(606, 29), cc.v2(561, 161), cc.v2(199, 261), cc.v2(-37, 303), cc.v2(-261, 262), cc.v2(-568, 165), cc.v2(-605, 32), cc.v2(-605, -100)];
			cardPosArray = [cc.v2(35, -245), cc.v2(416, -147), cc.v2(416, -19), cc.v2(373, 112), cc.v2(166, 112), cc.v2(-42, 154), cc.v2(-257, 116), cc.v2(-470, 116), cc.v2(-508, -16), cc.v2(-508, -148)];
			choosedArray = [1, 9, 2, 3, 4, 5, 6, 7, 8, 10];
		}
		headPosArray = this.fitIphone(headPosArray);
		cardPosArray = this.fitIphone(cardPosArray);
		for (let i = 0; i < chairCount; ++i) {
			let headNode = this.node.getChildByName('Head'+choosedArray[i]);
			let cardsNode = this.node.getChildByName('Cards'+choosedArray[i]);
			if (i > 0) {
				headNode.setPosition(headPosArray[i]);
				cardsNode.setPosition(cardPosArray[i]);
			}
			this.headNodeArray.push(headNode);
			headNode.getComponent('NNHead').setIndex(i);
			this.addHeadClickEvent(headNode.getChildByName('msgbg'));
			this.cardsNodeArray.push(cardsNode);
			cardsNode.getComponent('NNCards').setIndex(i);
		}
		for (let i = 1; i <= 10; ++i) {
			if (choosedArray.indexOf(i) == -1) {
				let headNode = this.node.getChildByName('Head'+i);
				let cardsNode = this.node.getChildByName('Cards'+i);
				if (headNode) { headNode.destroy(); }
				if (cardsNode) { cardsNode.destroy(); }
			}
		}
	},

	// 苹果iphonex位置特殊处理
	fitIphone: function (array) {
		let designWidth = 1334;
		let designHeight = 750;
		let visibleWidth = cc.visibleRect.width;
		let visibleHeight = cc.visibleRect.height;
		if (cc.visibleRect.width/cc.visibleRect.height > 1.8) {
			if (array.length == 6) {
				array[0] = cc.v2(-visibleWidth/2+array[0].x+designWidth/2, -visibleHeight/2+array[0].y+designHeight/2); // 左下 
				array[1] = cc.v2(visibleWidth/2-designWidth/2+array[1].x, array[1].y); // 右 
				array[2] = cc.v2(visibleWidth/2-designWidth/2+array[2].x, visibleHeight/2-designHeight/2+array[2].y); // 右上 
				array[3] = cc.v2(array[3].x, visibleHeight/2-designHeight/2+array[3].y); // 上 
				array[4] = cc.v2(-visibleWidth/2+array[4].x+designWidth/2, visibleHeight/2-designHeight/2+array[4].y); // 左上 
				array[5] = cc.v2(-visibleWidth/2+array[5].x+designWidth/2, array[5].y); // 左 
			}
			else if(array.length == 8) {
				array[0] = cc.v2(-visibleWidth/2+array[0].x+designWidth/2, -visibleHeight/2+array[0].y+designHeight/2); // 左下 
				array[1] = cc.v2(visibleWidth/2-designWidth/2+array[1].x, -visibleHeight/2+array[1].y+designHeight/2); // 右下
				array[2] = cc.v2(visibleWidth/2-designWidth/2+array[2].x, visibleHeight/2-designHeight/2+array[2].y); // 右上 
				array[3] = cc.v2(visibleWidth/2-designWidth/2+array[3].x, visibleHeight/2-designHeight/2+array[3].y); // 右上 
				array[4] = cc.v2(array[4].x, visibleHeight/2-designHeight/2+array[4].y); // 上 
				array[5] = cc.v2(-visibleWidth/2+array[5].x+designWidth/2, visibleHeight/2-designHeight/2+array[5].y); // 左上 
				array[6] = cc.v2(-visibleWidth/2+array[6].x+designWidth/2, visibleHeight/2-designHeight/2+array[6].y); // 左上 
				array[7] = cc.v2(-visibleWidth/2+array[7].x+designWidth/2, -visibleHeight/2+array[7].y+designHeight/2); // 左下
			}
			else if(array.length == 10) {
				array[0] = cc.v2(-visibleWidth/2+array[0].x+designWidth/2, -visibleHeight/2+array[0].y+designHeight/2); // 左下 
				array[1] = cc.v2(visibleWidth/2-designWidth/2+array[1].x, -visibleHeight/2+array[1].y+designHeight/2); // 右下
				array[2] = cc.v2(visibleWidth/2-designWidth/2+array[2].x, visibleHeight/2-designHeight/2+array[2].y); // 右上 
				array[3] = cc.v2(visibleWidth/2-designWidth/2+array[3].x, visibleHeight/2-designHeight/2+array[3].y); // 右上 
				array[4] = cc.v2(visibleWidth/2-designWidth/2+array[4].x, visibleHeight/2-designHeight/2+array[4].y); // 右上 
				array[5] = cc.v2(array[5].x, visibleHeight/2-designHeight/2+array[5].y); // 上 
				array[6] = cc.v2(-visibleWidth/2+array[6].x+designWidth/2, visibleHeight/2-designHeight/2+array[6].y); // 左上 
				array[7] = cc.v2(-visibleWidth/2+array[7].x+designWidth/2, visibleHeight/2-designHeight/2+array[7].y); // 左上 
				array[8] = cc.v2(-visibleWidth/2+array[8].x+designWidth/2, visibleHeight/2-designHeight/2+array[8].y); // 左上 
				array[9] = cc.v2(-visibleWidth/2+array[9].x+designWidth/2, -visibleHeight/2+array[9].y+designHeight/2); // 左下
			}
		}
		return array;
	},
});
